package com.example.android_wx.http;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;

public class HttpHelp {

    private static volatile HttpHelp httpHelp;

    private String apiUrl = "";
    private String queryUrl = "";
    private HashMap<String, String> common = null;

    public static HttpHelp get() {
        if (httpHelp == null) {
            synchronized (HttpHelp.class) {
                if (httpHelp == null) {
                    httpHelp = new HttpHelp();
                }
            }
        }
        return httpHelp;
    }

    public void init(Application app, String url, HashMap<String, String> hashMap) {

        this.apiUrl = url;
        this.common = hashMap;
        OkGo.getInstance().init(app);

    }

    public HttpHelp ready(String controller, String action) {

        queryUrl = apiUrl + controller + "/" + action + "?";
        return httpHelp;

    }

    public HttpHelp add(String key, String value) {

        queryUrl += key + "=" + value + "&";
        return httpHelp;

    }

    public void get(BaseCallBack baseCallBack) {
        for (String key : common.keySet()) {
            String value = common.get(key);
            queryUrl += key + "=" + value + "&";
        }
        System.out.println(queryUrl);
        OkGo.<String>get(queryUrl).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                try {
                    JSONObject jsonObject = new JSONObject(body);//将JSON字符串赋值给JSON对象
                    String code = jsonObject.getString("code");//获取code字段的值
                    String msg = jsonObject.getString("msg");//获取msg字段的值
                    //如果服务端返回失败
                    if (code.equals("2")) {
                        if (baseCallBack != null) {
                            baseCallBack.onFailure(msg);
                            return;
                        }
                    }
                    //如果服务端请求成功
                    if (baseCallBack != null) {
                        baseCallBack.onSuccess(jsonObject.getString("data"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (baseCallBack != null) {
                    baseCallBack.onFailure(response.message());
                }
            }
        });
    }

    public void post(File file, BaseCallBack baseCallBack) {
        for (String key : common.keySet()) {
            String value = common.get(key);
            queryUrl += key + "=" + value + "&";
        }
        System.out.println(queryUrl);
        OkGo.<String>post(queryUrl).params("image", file).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                try {
                    JSONObject jsonObject = new JSONObject(body);//将JSON字符串赋值给JSON对象
                    String code = jsonObject.getString("code");//获取code字段的值
                    String msg = jsonObject.getString("msg");//获取msg字段的值
                    //如果服务端返回失败
                    if (code.equals("2")) {
                        if (baseCallBack != null) {
                            baseCallBack.onFailure(msg);
                            return;
                        }
                    }
                    //如果服务端请求成功
                    if (baseCallBack != null) {
                        baseCallBack.onSuccess(jsonObject.getString("data"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (baseCallBack != null) {
                    baseCallBack.onFailure(response.message());
                }
            }
        });
    }

    public void setToken(String token) {

        common.put("token", token);

    }

}
